package org.jeecg.modules.yw.service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.jeecg.modules.yw.entity.FlPrice;
import org.jeecg.modules.yw.entity.PriceRules;
import org.jeecg.modules.yw.entity.PriceRulesEnterprise;
import org.jeecg.modules.yw.mapper.PriceRulesEnterpriseMapper;
import org.jeecg.modules.yw.mapper.PriceRulesMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class PriceRulesService extends ServiceImpl<PriceRulesMapper, PriceRules> {	
		@Resource 
		private PriceRulesMapper rulesMapper;	
		@Resource 
		private PriceRulesEnterpriseMapper rulesEnterpriseMapper;	
		public Map<Integer, List<PriceRules>> getAllRulues() {
	        QueryWrapper<PriceRules> queryWrapper = new QueryWrapper<>();
	        queryWrapper.orderByAsc("type");
			List<PriceRules> list = rulesMapper.selectList(queryWrapper);			
			Map<Integer, List<PriceRules>> map = new LinkedHashMap<Integer, List<PriceRules>>();			
			for (PriceRules obj: list) {
				List<PriceRules> rList = map.get(obj.getType());
				if (rList == null || rList.size() == 0) {
					List<PriceRules> temp = new ArrayList<>();
					temp.add(obj);
					map.put(obj.getType(), temp);
				} else {
					rList.add(obj);
					map.put(obj.getType(), rList);
				}
		   }			
		   return map;			
	  }
		
	  public Map<Integer, List<PriceRulesEnterprise>> getAllRuluesByEnterpriseId(String enterpriseId) {
	        QueryWrapper<PriceRulesEnterprise> queryWrapper = new QueryWrapper<>();
	        queryWrapper.eq("enterprise_id", enterpriseId);
	        queryWrapper.orderByAsc("type");
			List<PriceRulesEnterprise> list = rulesEnterpriseMapper.selectList(queryWrapper);			
			Map<Integer, List<PriceRulesEnterprise>> map = new LinkedHashMap<Integer, List<PriceRulesEnterprise>>();			
			for (PriceRulesEnterprise obj: list) {
				List<PriceRulesEnterprise> rList = map.get(obj.getType());
				if (rList == null || rList.size() == 0) {
					List<PriceRulesEnterprise> temp = new ArrayList<>();
					temp.add(obj);
					map.put(obj.getType(), temp);
				} else {
					rList.add(obj);
					map.put(obj.getType(), rList);
				}
		   }			
		   return map;			
	  }	
	  
	  public List<PriceRulesEnterprise> getAllRuleListByEnterpriseId(String enterpriseId) {
	      QueryWrapper<PriceRulesEnterprise> queryWrapper = new QueryWrapper<>();
	      queryWrapper.eq("enterprise_id", enterpriseId);
	      queryWrapper.orderByAsc("type");
		  List<PriceRulesEnterprise> list = rulesEnterpriseMapper.selectList(queryWrapper);	
		  return list;
	  }
		
		
	 public void insertInitiatePrice(String enterpriseId) throws Exception {
		 rulesEnterpriseMapper.deleteRulesByEnterpriseId(enterpriseId);
         QueryWrapper<PriceRules> queryWrapper = new QueryWrapper<>();
         queryWrapper.orderByAsc("type");
		 List<PriceRules> list = rulesMapper.selectList(queryWrapper);
		 for (PriceRules obj : list) {
			 PriceRulesEnterprise enterprise = new PriceRulesEnterprise();
			 enterprise.setChoice(obj.getChoice());
			 enterprise.setType(obj.getType());
			 enterprise.setDesc(obj.getDesc());
			 enterprise.setGreatThan(obj.getGreatThan());
			 enterprise.setLessThan(obj.getLessThan());
			 enterprise.setPrice(obj.getPrice());			 
			 enterprise.setEnterpriseId(enterpriseId);			 
			 rulesEnterpriseMapper.insert(enterprise);
		 }		 
	 }
	 
	 
	 public void updateEnterRulePrisePrice(String enterpriseId, List<PriceRulesEnterprise> list) throws Exception {
		 rulesEnterpriseMapper.deleteRulesByEnterpriseId(enterpriseId);
		 for (PriceRulesEnterprise obj : list) {
			 obj.setEnterpriseId(enterpriseId);			 
			 rulesEnterpriseMapper.insert(obj);
		 }		 
	 }
		
	 /**
	  * 类型
	  * @param type
	  */
	  public String getPriceByRule(Integer type, String area, Integer fl, Integer sfjj, String jjlx, Map<Integer, List<PriceRules>> map, FlPrice flPrice) {
		   List<PriceRules> list = map.get(type);
		   String price = "0";
		   String option = "A";
		   for (PriceRules obj : list) {			   
			    String greaterThan = obj.getGreatThan();
			    String lessThan = obj.getLessThan();			   
			    BigDecimal areaB = new BigDecimal(area);
			    BigDecimal greater = new BigDecimal(greaterThan);
			    BigDecimal less = new BigDecimal(lessThan);			    
			    if (!lessThan.equals("-1")) {
			    	if (areaB.compareTo(greater) >= 0 && areaB.compareTo(less) <= 0) {
			    		price = obj.getPrice();
			    		option = obj.getChoice();
			    		break;
			    	}
			    } else {
			    	if (areaB.compareTo(greater) >= 0) {
			    		price = obj.getPrice();
			    		option = obj.getChoice();
			    		break;
			    	}
			    }	
		   }		   
		   if (type == 1 || type == 2) {
			   if (option.equals("C")) {
				   if (fl == 1) {					  
					   price = new BigDecimal(price).add(new BigDecimal(flPrice.getHflPrice())).toString();
					   if (sfjj == 1 && jjlx.contains("3")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getQtjhflPrice())).toString();
					   }
				   } else {
					   if (sfjj == 1 && jjlx.contains("3")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getQtjbhflPrice())).toString();
					   }
				   }
				   price = new BigDecimal(price).multiply(new BigDecimal(area)).toString();
		       }
		   } else if (type == 3) {
			   if (option.equals("C")) {
				   if (fl == 1) {			
					   price = new BigDecimal(price).add(new BigDecimal(flPrice.getHflPrice())).toString();
					   if (sfjj == 1 && jjlx.contains("1")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getNbljhflPrice())).toString();
					   } 
					   if (sfjj == 1 && jjlx.contains("2")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getWbljhflPrice())).toString();
					   } 
					   if (sfjj == 1 && jjlx.contains("3")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getQtjhflPrice())).toString();
					   }
				   } else {
					   if (sfjj == 1 && jjlx.contains("1")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getNbljbhflPrice())).toString();
					   } 
					   if (sfjj == 1 && jjlx.contains("2")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getWbljbhflPrice())).toString();
					   }
					   if (sfjj == 1 && jjlx.contains("3")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getQtjbhflPrice())).toString();
					   } 
				   }
				   price = new BigDecimal(price).multiply(new BigDecimal(area)).toString();
			   }
		   } else if (type == 4) {
			   if (option.equals("C")) {
				   if (fl == 1) {			
					   price = new BigDecimal(price).add(new BigDecimal(flPrice.getHflPrice())).toString();
					   if (sfjj == 1 && jjlx.contains("1")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqnbljhflPrice())).toString();
					   } 
					   if (sfjj == 1 && jjlx.contains("2")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqwbljhflPrice())).toString();
					   }
					   if (sfjj == 1 && jjlx.contains("3")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqnqtjhflPrice())).toString();
					   }
				   } else {
					   if (sfjj == 1 && jjlx.contains("1")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqnbljbhflPrice())).toString();
					   } 
					   if (sfjj == 1 && jjlx.contains("2")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqwbljbhflPrice())).toString();
					   } 
					   if (sfjj == 1 && jjlx.contains("3")) {
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqnqtjbhflPrice())).toString();
					   } 
				   }
				   price = new BigDecimal(price).multiply(new BigDecimal(area)).toString();
			   }
		   } else if (type == 5 || type == 6 || type == 7 || type == 8 || type == 9) {
			   //  5. 阳光房 6 采光井
			   price = new BigDecimal(price).multiply(new BigDecimal(area)).toString();
		   }		   
		   
		   return new BigDecimal(price).setScale(2, RoundingMode.HALF_UP).toString();		   
	   }
	  
	    /**
		  * 类型
		  * @param type
		  */
		  public String getPriceByRuleEnterprise(Integer type, String area, Integer fl, Integer sfjj, String jjlx, Map<Integer, List<PriceRulesEnterprise>> map, FlPrice flPrice) {
			   List<PriceRulesEnterprise> list = map.get(type);
			   String price = "0";
			   String option = "A";
			   for (PriceRulesEnterprise obj : list) {			   
				    String greaterThan = obj.getGreatThan();
				    String lessThan = obj.getLessThan();			   
				    BigDecimal areaB = new BigDecimal(area);
				    BigDecimal greater = new BigDecimal(greaterThan);
				    BigDecimal less = new BigDecimal(lessThan);			    
				    if (!lessThan.equals("-1")) {
				    	if (areaB.compareTo(greater) >= 0 && areaB.compareTo(less) <= 0) {
				    		price = obj.getPrice();
				    		option = obj.getChoice();
				    		break;
				    	}
				    } else {
				    	if (areaB.compareTo(greater) >= 0) {
				    		price = obj.getPrice();
				    		option = obj.getChoice();
				    		break;
				    	}
				    }	
			   }		   
			   if (type == 1 || type == 2) {
				   if (option.equals("C")) {
					   if (fl == 1) {					  
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getHflPrice())).toString();
						   if (sfjj == 1 && jjlx.contains("3")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getQtjhflPrice())).toString();
						   }
					   } else {
						   if (sfjj == 1 && jjlx.contains("3")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getQtjbhflPrice())).toString();
						   }
					   }
					   price = new BigDecimal(price).multiply(new BigDecimal(area)).toString();
			       }
			   } else if (type == 3) {
				   if (option.equals("C")) {
					   if (fl == 1) {			
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getHflPrice())).toString();
						   if (sfjj == 1 && jjlx.contains("1")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getNbljhflPrice())).toString();
						   } 
						   if (sfjj == 1 && jjlx.contains("2")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getWbljhflPrice())).toString();
						   } 
						   if (sfjj == 1 && jjlx.contains("3")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getQtjhflPrice())).toString();
						   }
					   } else {
						   if (sfjj == 1 && jjlx.contains("1")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getNbljbhflPrice())).toString();
						   } 
						   if (sfjj == 1 && jjlx.contains("2")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getWbljbhflPrice())).toString();
						   }
						   if (sfjj == 1 && jjlx.contains("3")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getQtjbhflPrice())).toString();
						   } 
					   }
					   price = new BigDecimal(price).multiply(new BigDecimal(area)).toString();
				   }
			   } else if (type == 4) {
				   if (option.equals("C")) {
					   if (fl == 1) {			
						   price = new BigDecimal(price).add(new BigDecimal(flPrice.getHflPrice())).toString();
						   if (sfjj == 1 && jjlx.contains("1")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqnbljhflPrice())).toString();
						   } 
						   if (sfjj == 1 && jjlx.contains("2")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqwbljhflPrice())).toString();
						   }
						   if (sfjj == 1 && jjlx.contains("3")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqnqtjhflPrice())).toString();
						   }
					   } else {
						   if (sfjj == 1 && jjlx.contains("1")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqnbljbhflPrice())).toString();
						   } 
						   if (sfjj == 1 && jjlx.contains("2")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqwbljbhflPrice())).toString();
						   } 
						   if (sfjj == 1 && jjlx.contains("3")) {
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqnqtjbhflPrice())).toString();
						   } 
					   }
					   price = new BigDecimal(price).multiply(new BigDecimal(area)).toString();
				   }
			   } else if (type == 5 || type == 6 || type == 7 || type == 8 || type == 9) {
				   //  5. 阳光房 6 采光井
				   price = new BigDecimal(price).multiply(new BigDecimal(area)).toString();
			   }	
			   
			   return new BigDecimal(price).setScale(2, RoundingMode.HALF_UP).toString();		   
		  }
		  
		  
		  
		     /**
			  * 返回价格和选项
			  * @param type
			  */
			  public String getPriceAndOptionByRuleEnterprise(Integer type, String area, Integer fl, Integer sfjj, String jjlx, Map<Integer, List<PriceRulesEnterprise>> map, FlPrice flPrice) {
				   List<PriceRulesEnterprise> list = map.get(type);
				   String price = "0";
				   String option = "A";
				   String hflPrice = "0";
				   String unitPrice = "0";
				   String fixedPrice = "0";
				   if(list==null){
					  return "";
				   }
				   for (PriceRulesEnterprise obj : list) {			   
					    String greaterThan = obj.getGreatThan();
					    String lessThan = obj.getLessThan();			   
					    BigDecimal areaB = new BigDecimal(area);
					    BigDecimal greater = new BigDecimal(greaterThan);
					    BigDecimal less = new BigDecimal(lessThan);			    
					    if (!lessThan.equals("-1")) {
					    	if (areaB.compareTo(greater) >= 0 && areaB.compareTo(less) <= 0) {
					    		price = obj.getPrice();
					    		option = obj.getChoice();
					    		break;
					    	}
					    } else {
					    	if (areaB.compareTo(greater) >= 0) {
					    		price = obj.getPrice();
					    		option = obj.getChoice();
					    		break;
					    	}
					    }	
				   }		   
				   unitPrice = price;
				   fixedPrice = price;
				   if (type == 1 || type == 2) {
					   if (option.equals("C")) {
						   if (fl == 1) {					  
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getHflPrice())).toString();							   
							   hflPrice = price;	
							   unitPrice = price;
							   if (sfjj == 1 && jjlx.contains("3")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getQtjhflPrice())).toString();
							   }
						   } else {
							   if (sfjj == 1 && jjlx.contains("3")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getQtjbhflPrice())).toString();
							   }
						   }
						   price = new BigDecimal(price).multiply(new BigDecimal(area)).toString();
				       }
				   } else if (type == 3) {
					   if (option.equals("C")) {
						   if (fl == 1) {			
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getHflPrice())).toString();
							   hflPrice = price;
							   unitPrice = price;
							   if (sfjj == 1 && jjlx.contains("1")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getNbljhflPrice())).toString();
							   } 
							   if (sfjj == 1 && jjlx.contains("2")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getWbljhflPrice())).toString();
							   } 
							   if (sfjj == 1 && jjlx.contains("3")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getQtjhflPrice())).toString();
							   }
						   } else {
							   if (sfjj == 1 && jjlx.contains("1")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getNbljbhflPrice())).toString();
							   } 
							   if (sfjj == 1 && jjlx.contains("2")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getWbljbhflPrice())).toString();
							   }
							   if (sfjj == 1 && jjlx.contains("3")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getQtjbhflPrice())).toString();
							   } 
						   }
						   price = new BigDecimal(price).multiply(new BigDecimal(area)).toString();
					   }
				   } else if (type == 4) {
					   if (option.equals("C")) {
						   if (fl == 1) {			
							   price = new BigDecimal(price).add(new BigDecimal(flPrice.getHflPrice())).toString();
							   hflPrice = price;
							   unitPrice = price;
							   if (sfjj == 1 && jjlx.contains("1")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqnbljhflPrice())).toString();
							   } 
							   if (sfjj == 1 && jjlx.contains("2")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqwbljhflPrice())).toString();
							   }
							   if (sfjj == 1 && jjlx.contains("3")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqnqtjhflPrice())).toString();
							   }
						   } else {
							   if (sfjj == 1 && jjlx.contains("1")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqnbljbhflPrice())).toString();
							   } 
							   if (sfjj == 1 && jjlx.contains("2")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getDqwbljbhflPrice())).toString();
							   } 
							   if (sfjj == 1 && jjlx.contains("3")) {
								   price = new BigDecimal(price).add(new BigDecimal(flPrice.getQtjbhflPrice())).toString();
							   } 
						   }
						   price = new BigDecimal(price).multiply(new BigDecimal(area)).toString();
					   }
				   } else if (type == 5 || type == 6 || type == 7 || type == 8 || type == 9) {
					   //  5. 阳光房 6 采光井
					   price = new BigDecimal(price).multiply(new BigDecimal(area)).toString();
				   }
				   return new BigDecimal(price).setScale(2, RoundingMode.HALF_UP).toString() +","+option+","+hflPrice+","+unitPrice+","+fixedPrice;		   
			  }
		  
}
